#include <ESP32Servo.h>

#include <WiFi.h>
const char* ssid     = "kevin";
const char* password = "12345678";

WiFiServer server(80);

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
// 16 servo objects can be created on the ESP32

int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 
// Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42
// Possible PWM GPIO pins on the ESP32-S3: 0(used by on-board button),1-21,35-45,47,48(used by on-board LED)
// Possible PWM GPIO pins on the ESP32-C3: 0(used by on-board button),1-7,8(used by on-board LED),9-10,18-21
int servoPin1 = 9;
int servoPin2 = 10;
int servoPin3 = 18;
int servoPin4 = 19;



void setup() {
  Serial.begin(115200);           //打开串口

  //设置PWM输出定时器
	// Allow allocation of all timers
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
  //设置Servo对象
	myservo1.setPeriodHertz(50);    // standard 50 hz servo
	myservo1.attach(servoPin1, 1000, 2000); // attaches the servo on pin 9 to the servo object
  myservo2.setPeriodHertz(50);    // standard 50 hz servo
	myservo2.attach(servoPin2, 1000, 2000); // attaches the servo on pin 10 to the servo object
  myservo3.setPeriodHertz(50);    // standard 50 hz servo
	myservo3.attach(servoPin3, 1000, 2000); // attaches the servo on pin 18 to the servo object
  myservo4.setPeriodHertz(50);    // standard 50 hz servo
	myservo4.attach(servoPin4, 1000, 2000); // attaches the servo on pin 19 to the servo object
	// using default min/max of 1000us and 2000us
	// different servos may require different min/max settings
	// for an accurate 0 to 180 sweep

  //连接wifi热点
  Serial.println("start now");
    // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    //启动web server
    server.begin();
}

void loop() {
   WiFiClient client = server.available();   // listen for incoming clients
 if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html; charset=utf-8");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<div style='font-size:40px;text-align:center;margin:20 auto;'>------<a href=\"/Z\">抓取</a>------<br/><br/>");
            client.print("---<a href=\"/0\">0</a>------");
            client.print("<a href=\"/180\">放开</a>---<br><br/>");
            client.print("<a href=\"/90\">------半开------</a><br/><br/>");

            client.print("腰------<a href=\"/A\">0</a>------<br/><br/>");
            client.print("<------<a href=\"/B\">18</a>------<br/><br/>");
            client.print("<------<a href=\"/C\">36</a>------<br/><br/>");
            client.print("<------<a href=\"/D\">54</a>------<br/><br/>");
            client.print("<------<a href=\"/E\">72</a>------<br/><br/>");
            client.print("<------<a href=\"/F\">90</a>------<br/><br/>");
            client.print("<------<a href=\"/G\">108</a>------<br/><br/>");
            client.print("<------<a href=\"/H\">126</a>------<br/><br/>");
            client.print("<------<a href=\"/I\">144</a>------<br/><br/>");
            client.print("<------<a href=\"/J\">162</a>------<br/><br/>");
            client.print("<------<a href=\"/K\">180</a>------<br/><br/>");
            client.print("底------<a href=\"/L\">0</a>------<br/><br/>");
            client.print("<------<a href=\"/M\">18</a>------<br/><br/>");
            client.print("<------<a href=\"/N\">36</a>------<br/><br/>");
            client.print("<------<a href=\"/O\">54</a>------<br/><br/>");
            client.print("<------<a href=\"/P\">72</a>------<br/><br/>");
            client.print("<------<a href=\"/Q\">90</a>------<br/><br/>");
            client.print("<------<a href=\"/R\">108</a>------<br/><br/>");
            client.print("<------<a href=\"/S\">126</a>------<br/><br/>");
            client.print("<------<a href=\"/T\">144</a>------<br/><br/>");
            client.print("<------<a href=\"/U\">162</a>------<br/><br/>");
            client.print("<------<a href=\"/V\">180</a>------<br/><br/>");
            client.print("颈------<a href=\"/!\">0</a>------<br/><br/>");
            client.print("<------<a href=\"/@\">18</a>------<br/><br/>");
            client.print("<------<a href=\"/#\">36</a>------<br/><br/>");
            client.print("<------<a href=\"/$\">54</a>------<br/><br/>");
            client.print("<------<a href=\"/%\">72</a>------<br/><br/>");
            client.print("<------<a href=\"/^\">90</a>------<br/><br/>");
            client.print("<------<a href=\"/&\">108</a>------<br/><br/>");
            client.print("<------<a href=\"/*\">126</a>------<br/><br/>");
            client.print("<------<a href=\"/(\">144</a>------<br/><br/>");
            client.print("<------<a href=\"/)\">162</a>------<br/><br/>");
            client.print("<------<a href=\"/-\">180</a>------<br/><br/>");
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /Z")) {
		      myservo3.write(30);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /0")) {
		      myservo3.write(0);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /90")) {
		      myservo3.write(90);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /180")) {
		      myservo3.write(180);    // tell servo to go to position in variable 'pos'
        }
         if (currentLine.endsWith("GET /A")) {
		      myservo2.write(0);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /B")) {
		      myservo2.write(18);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /C")) {
		      myservo2.write(36);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /D")) {
		      myservo2.write(54);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /E")) {
		      myservo2.write(72);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /F")) {
		      myservo2.write(90);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /G")) {
		      myservo2.write(108);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /H")) {
		      myservo2.write(126);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /I")) {
		      myservo2.write(144);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /J")) {
		      myservo2.write(162);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /K")) {
		      myservo2.write(180);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /L")) {
		      myservo1.write(0);    // tell servo to go to position in variable 'pos'
      }if (currentLine.endsWith("GET /M")) {
		      myservo1.write(18);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /N")) {
		      myservo1.write(36);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /O")) {
		      myservo1.write(54);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /P")) {
		      myservo1.write(72);    // tell servo to go to position in variable 'pos'
      }
      if (currentLine.endsWith("GET /Q")) {
		      myservo1.write(90);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /R")) {
		      myservo1.write(108);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /S")) {
		      myservo1.write(126);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /T")) {
		      myservo1.write(144);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /U")) {
		      myservo1.write(162);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /V")) {
		      myservo1.write(180);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /!")) {
		      myservo4.write(0);    // tell servo to go to position in variable 'pos'
      }
      if (currentLine.endsWith("GET /@")) {
		      myservo4.write(18);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /#")) {
		      myservo4.write(36);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /$")) {
		      myservo4.write(54);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /%")) {
		      myservo4.write(72);    // tell servo to go to position in variable 'pos'
      }
      if (currentLine.endsWith("GET /^")) {
		      myservo4.write(90);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /&")) {
		      myservo4.write(108);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /*")) {
		      myservo4.write(126);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /(")) {
		      myservo4.write(144);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /)")) {
		      myservo4.write(162);    // tell servo to go to position in variable 'pos'
        }
        if (currentLine.endsWith("GET /-")) {
		      myservo4.write(180);    // tell servo to go to position in variable 'pos'
        }
    }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

